//
//  DetailTableViewController.m
//  B5MDetailFramework
//
//  Created by boguang on 15/8/21.
//  Copyright (c) 2015年 micker. All rights reserved.
//

#import "DetailTableViewController.h"

#import "DetailView.h"

#import "BuyBottomView.h"
#import "DetailViewCell.h"
#import "BuyTopImageCell.h"
#import "STPhotoBroswer.h"
#import "ImageBrowserViewController.h"


#define screenH [UIScreen mainScreen].bounds.size.height
#define screenW [UIScreen mainScreen].bounds.size.width
#define Time 0.3
#define ViewHeight  60

//static NSString *titles[] = {@"图文详情",@"商品评论",@"店铺推荐"};


@interface DetailTableViewController () <DetailViewSectionDelegate,  UITableViewDataSource, UITableViewDelegate>
{
    NSArray *titles;
}
@property (weak, nonatomic) BuyBottomView* bottomView;
@property (nonatomic, strong) DetailView *detailView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UITableView *tableViewTwo;
@property (nonatomic, strong) NSMutableArray *data;

@property (nonatomic, strong)JXRollView *rollViewColor;
@property (weak, nonatomic) BuyTopImageCell* imageView;

@property (nonatomic, strong)UIView *viewBigImage;
@property (nonatomic, strong)UIView *viewImageBack;
@property (nonatomic, strong)NSArray *arrImgs;


@end

@implementation DetailTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initView];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:self.detailView];
    self.view.backgroundColor = [UIColor lightGrayColor];
    [self.tableView reloadData];
    [self.detailView reloadData];
}

- (void) dealloc {
    NSLog(@"DetailTableViewController dealloc");

}

- (DetailView *) detailView {
    if (!_detailView) {
        _detailView = [[DetailView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height-64)];
        _detailView.delegate = self;
//        _detailView.topScrollPageView.delegate = self;
    }
    return _detailView;
}
- (void)initView{
    
    NSString *comment = NSLocalizedString(@"comment", nil);
    NSString *standard = NSLocalizedString(@"standard", nil);
    NSString *detail = NSLocalizedString(@"detail", nil);
    titles  = @[comment,standard,detail];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, screenW, screenH)];
    [_tableView registerNib:[UINib nibWithNibName:@"DetailViewCell" bundle:nil] forCellReuseIdentifier:@"DetailViewCell"];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    BuyBottomView* bottomView = [BuyBottomView view];
    self.bottomView = bottomView;
    _tableView.tableFooterView = self.bottomView;
    
    BuyTopImageCell* imageView = [[[NSBundle mainBundle] loadNibNamed:@"BuyTopImageCell" owner:nil options:nil] lastObject];
    self.imageView = imageView;
    _tableView.tableHeaderView = self.imageView;
    
    
    NSString *img1 = @"ad";
    NSString *img2 = @"ae";
    NSString *img3 = @"af";
    NSString *img4 = @"ac";
    _arrImgs = [NSArray arrayWithObjects:[UIImage imageNamed:img1],[UIImage imageNamed:img2],[UIImage imageNamed:img3],[UIImage imageNamed:img4], nil];
    
    
    [imageView setClickImageBigBlock:^(NSInteger index) {
        
//        // 点击打开大图片
//        [self createView];
        
//        STPhotoBroswer * broser = [[STPhotoBroswer alloc]initWithImageArray:@[[UIImage imageNamed:@"ac.jpg"],[UIImage imageNamed:@"ad.jpg"],[UIImage imageNamed:@"ae.jpg"],[UIImage imageNamed:@"af.jpg"]] currentIndex:0];
//        [broser show];
        
        [ImageBrowserViewController show:self type:PhotoBroswerVCTypeModal index:index imagesBlock:^NSArray *{
            return _arrImgs;
        }];
        
    }];
    
    [_tableView registerNib:[UINib nibWithNibName:@"DetailViewCell" bundle:nil] forCellReuseIdentifier:@"DetailViewCell"];
    [_tableView registerNib:[UINib nibWithNibName:@"BottomCell" bundle:nil] forCellReuseIdentifier:@"BottomCell"];
    
    
}
// 点击打开大图片
-(void)createView{
    
    self.viewBigImage = [[UIView alloc]initWithFrame:self.view.frame];
    self.viewImageBack = [[UIView alloc]initWithFrame:self.view.frame];
    self.viewBigImage.backgroundColor = [UIColor clearColor];
    self.viewImageBack.backgroundColor = [UIColor blackColor];
    self.viewImageBack.alpha = 0;
    
    [self.view addSubview:self.viewBigImage];
    self.rollViewColor = [[JXRollView alloc] initWithFrame:CGRectMake(0, 0, self.viewBigImage.frame.size.width, 277)];
    [self.viewBigImage addSubview:_viewImageBack];
    [self.viewBigImage addSubview:self.rollViewColor];
    
    [UIView animateWithDuration:Time animations:^{
        self.viewImageBack.alpha = 1.0;
        self.rollViewColor.frame = CGRectMake(0, (screenH-self.rollViewColor.frame.size.height)/2, screenW, 277);
    }];
    
    
    [self lunbo];
}

// 打开大图片
-(void)lunbo{
    
    NSString *img1 = @"ad";
    NSString *img2 = @"ae";
    NSString *img3 = @"af";
    _arrImgs = [NSArray arrayWithObjects:img1,img2,img3, nil];
    
    self.rollViewColor.pageIndicatorColor = [UIColor lightGrayColor];
    self.rollViewColor.currentPageIndicatorColor = [UIColor redColor];
    self.rollViewColor.delegate = self;
    self.rollViewColor.autoRolling = NO;
    [self.rollViewColor reloadData];
    
    
}
//- (UITableView *) tableView {
//    if (!_tableView) {
//        _tableView = [[UITableView alloc] initWithFrame:_detailView.bounds];
//        [_tableView registerNib:[UINib nibWithNibName:@"DetailViewCell" bundle:nil] forCellReuseIdentifier:@"DetailViewCell"];
//        _tableView.delegate = self;
//        _tableView.dataSource = self;
//    }
//    return _tableView;
//}
#pragma mark DetailViewSectionDelegate


- (UIView *) viewAtTop {
    return self.tableView;
}

- (NSUInteger ) numberOfSections {
    return 3;
}


- (NSString *) titleOfSectionAt:(NSUInteger )index {
    return titles[index];
}

- (UIView *) viewOfSectionAt:(NSUInteger ) index {
    UIWebView *webview = [[UIWebView alloc] initWithFrame:CGRectZero];
    UITableView *tbl = [[UITableView alloc]initWithFrame:CGRectZero];
    UIView *vvv;
    switch (index) {
        case 0:
                vvv = webview;
            break;
        case 1:
            vvv = tbl;
            break;
        case 2:
            vvv = tbl;
            break;
            
        default:
            break;
    }
    return vvv;
}

- (void) didChangeToSection:(NSUInteger) index view:(UIView *) view {
    
    NSString *url = @"http://www.mulazim.com";
    UIWebView *webView;
    UITableView *tableTwo;

    switch (index) {
        case 0:
            webView = (UIWebView *) view;
            if (!webView.request) {
                [webView stopLoading];
                [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
            }
            break;
        case 1:
            tableTwo = (UITableView *)view;

            self.tableViewTwo = tableTwo;
            tableTwo.dataSource = self;
            tableTwo.delegate = self;
            [tableTwo reloadData];
            
            [_tableViewTwo registerNib:[UINib nibWithNibName:@"DetailViewCell" bundle:nil] forCellReuseIdentifier:@"DetailViewCell"];
            break;
        case 2:
            tableTwo = (UITableView *)view;
            
            self.tableViewTwo = tableTwo;
            tableTwo.dataSource = self;
            tableTwo.delegate = self;
            [tableTwo reloadData];
            
            [_tableViewTwo registerNib:[UINib nibWithNibName:@"DetailViewCell" bundle:nil] forCellReuseIdentifier:@"DetailViewCell"];
            break;
            
        default:
            break;
    }
    
    
//    UIView *vvv =  [[UIView alloc]initWithFrame:CGRectMake(0, 100, 300, 300)];
//    vvv.backgroundColor = [UIColor redColor];
//    [self.view addSubview:vvv];
    
    
//    ABViewController *ad = [[ABViewController alloc]initWithNibName:@"ABViewController" bundle:nil];
//    [self addChildViewController:ad];
//    ad.view.frame=CGRectMake(0, self.view.bounds.size.height, self.view.bounds.size.width, self.view.bounds.size.height);
//    [UIView animateWithDuration:1.0 animations:^{
//        ad.view.frame=CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
//    }];
    
}


#pragma mark ==
#pragma mark == tableView



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([tableView isEqual:self.tableView]) {
        return 5;
    }else{
        return 10;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    DetailViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailViewCell"];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    NSString *classString = self.data[indexPath.row][@"class"];
//    Class newClass = NSClassFromString(classString);
//    UIViewController *controller = [[newClass alloc] init];
//    controller.title = self.data[indexPath.row][@"title"];
//    if (controller)
//        [self.navigationController pushViewController:controller animated:YES];
    
//    ABViewController *ad = [[ABViewController alloc]initWithNibName:@"ABViewController" bundle:nil];
//    [self.navigationController pushViewController:ad animated:YES];
    
}
#pragma mark <JXRollViewDelegate>
- (NSInteger)numberOfItemsInRollView:(JXRollView *)rollView {
    return _arrImgs.count;
}

- (void)rollView:(JXRollView *)rollView setImageForImageView:(UIImageView *const)imageView atIndex:(NSInteger)index {
    
    
    imageView.image = [UIImage imageNamed:_arrImgs[index]];
    
}

- (void)rollView:(JXRollView *)rollView didTapItemAtIndex:(NSInteger)index {
    NSLog(@"Tap the index %ld", index);
    
    [UIView animateWithDuration:Time animations:^{
        self.viewImageBack.alpha = 0;
        self.rollViewColor.frame = CGRectMake(0, 64, screenW, 277);
    }completion:^(BOOL finished) {
        [self.viewBigImage removeFromSuperview];
    }];
}
@end
